package ui;
import java.io.InputStream;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Composite;

import each_class.RanKingClass;
import each_class.SongClass;
import ui.global.GraphicsUtil;

public class MusicList extends Composite implements MouseTrackListener{

	 private Button playButton;
	public MusicList(Composite parent, RanKingClass musicList) {
		super(parent, SWT.NONE);
		Region region =  GraphicsUtil.createRoundedRectangle(0, 0, 256, 256, 20);	
		setRegion(region);
		playButton = new Button(this, SWT.NONE) {
			
			@Override
			public void mouseUp(MouseEvent e) {
				List<SongClass> list = musicList.getSongList();
				if (list != null) {
					ui.global.Current.getMusicListUI().active(list);
				}
			}
		};
		playButton.initial(
				new Image(null, "src/main/java/image/playlsit_button_cover_deactive.png"),
				new Image(null, "src/main/java/image/playlsit_button_cover_active.png"));
		playButton.setBounds(96, 96, 64, 64);
		playButton.setRegion(ui.global.GraphicsUtil.handleTransparenceRegion(new Image(null, "src/main/java/image/playlsit_button_cover_active.png"), 64, 64));
		playButton.setVisible(true);
		addMouseTrackListener(this);
		//setBackgroundImage(SWTResourceManager.getImage(MusicList.class, "src/main/java/image/no_picture_x256.jpegg"));
		setBackgroundImage(new Image(null, "src/main/java/image/no_picture_x256.jpeg"));
		new Thread() {
			public void run() {
				InputStream inputStream =  ui.global.Operation.getMusicListCover(musicList);
				if (inputStream != null) {
					Image cover = new Image(null, inputStream);
					Image cover1 = GraphicsUtil.resize(cover, 256, 256);
					getDisplay().asyncExec(new Runnable() {
						
						@Override
						public void run() {
							setBackgroundImage(cover1);
						}
					});
				}
				
			};
		}.start();
	}

	
	
	@Override
	public void mouseEnter(MouseEvent e) {
		if (e.x < 25|| e.y < 25 || e.x > getBounds().width - 10 || e.y > getBounds().height - 10 ) {
			new Thread() {
				public void run() {
					for(int i = 0; i <= 5; i ++) {
						getDisplay().asyncExec(new Runnable() {			
							@Override
							public void run() {
								setLocation(getLocation().x, getLocation().y - 1);
							}
						});
						try {
							Thread.sleep(20L);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					getDisplay().asyncExec(new Runnable() {			
						@Override
						public void run() {
							playButton.setVisible(true);
							GC gc = new GC(playButton.getParent());
							gc.setAlpha(100);
							gc.setBackground(ui.global.UIColor.BLACK);
							gc.setLineWidth(256);
							gc.drawLine(0, 127, 256, 127);
							gc.dispose();	
						}
					});
				}
			}.start();
			
		}
		
	}

	@Override
	public void mouseExit(MouseEvent e) {
		if (e.x < 0 || e.y < 0 || e.x >= getBounds().width || e.y > getBounds().height - 11) {
			new Thread() {
				public void run() {
					for(int i = 0; i <= 5; ++i) {
						getDisplay().asyncExec(new Runnable() {			
							@Override
							public void run() {
								setLocation(getLocation().x, getLocation().y + 1);
							}
						});
						try {
							Thread.sleep(20L);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}.start();
			playButton.setVisible(false);
			redraw();
		}
	}

	@Override
	public void mouseHover(MouseEvent e) {
	}
}
